var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var Pagination = require('../../utils/pagination.js');
var app = getApp();
Page({
    data: {
        dataLoaded: false,
        shopInfo: {},
        userInfo: {}
    },

    onLoad(options) {
        var _self = this;
        var text = '';
        // var backgroundAudioManager = wx.getBackgroundAudioManager();
        // var voiceData = 'http://api.y.88ff.com/asset/audio/notify.mp3'; // 设置了 src 之后会自动播放
        app.getUserInfo(function(userInfo) {
            _self.setData({
                userInfo
            });
        });
        this._getShopData(function() {
            var url = '/openapi/Shop/newOrderNotify';
            var paramsData = {
                shopSid: app.getShopSid()
            }

            setInterval(function() {
                ajax.P(url, paramsData, function(data) {
                    if (data.result.order_id != false) {
                        text = '您有一个新订单，请及时处理';
                        // backgroundAudioManager.src = voiceData;
                    } else {
                        text = '暂无新订单信息';
                    }
                    wx.setTopBarText({
                        text: text
                    })
                })
            }, 10000);
        });
    },
    _getShopData: function(cb) {
        var _self = this;
        var url = "/openapi/Shop/shopIndex";
        var paramsData = {
            shopSid: app.getShopSid()
        }
        ajax.P(url, paramsData, function(data) {
            _self.setData({
                shopInfo: data.result,
                dataLoaded: true
            })
            cb && cb();
        })
    },
    _openQRCode: function() {
        var img = this.data.shopInfo.xcx_qrcode;
        var imgs = [];
        imgs.push(img);
        wx.previewImage({
            current: img,
            urls: imgs
        })
    },
    _jump(evt) {
        var url = evt.currentTarget.dataset.url;
        wx.navigateTo({
            url
        })
    }
});